from direct.gui.DirectGui import *
from pandac.PandaModules import TextNode
import __builtin__


class GuiManager(object):
    def __init__(self):
        self.curGUI = {}
        self.curGUI.update({"Text": OnscreenText(text='', pos=(-1.3, 0.9), scale=0.05,align=TextNode.ALeft)})

        self.last = 0
        self.time = 1

        self.update_clients()
        self.update_sessions()
        self.update_running()

    def update(self, dt):
        self.last += dt

        if self.last >= self.time:
            self.last = 0
            self.update_clients()
            self.update_sessions()
            self.update_running()
            self.curGUI["Text"].flattenStrong()

    def update_clients(self):
        self.curGUI["Text"]["text"] = "CLIENTS\n\n"

        for u in __builtin__.base.networkmanager.connected_players:
            self.curGUI["Text"]["text"] += "    -> NAME: " + str(u.name) + "    ID: " + str(u.peer.addr[1]) + "     IP: " + str(u.peer.addr[0]) + "\n"

    def update_sessions(self):
        self.curGUI["Text"]["text"] += "\n\n\nOPEN SESSIONS\n\n"

        for s in __builtin__.base.sessionmanager.sessions:
            if not s.started:
                self.curGUI["Text"]["text"] += "    -> ID: " + str(s.ID) + "\n"
                if s.attacker != 0:
                    self.curGUI["Text"]["text"] += "        -> ATK: " + str(s.attacker.peer.addr[1]) + "/" + str(s.attacker.name) + "\n"
                if s.defender != 0:
                    self.curGUI["Text"]["text"] += "        -> DEF: " + str(s.defender.peer.addr[1]) + "/" + str(s.defender.name) + "\n"
                self.curGUI["Text"]["text"] += "\n"

    def update_running(self):
        self.curGUI["Text"]["text"] += "\n\n\nRUNNING SESSIONS\n\n"

        for s in __builtin__.base.sessionmanager.sessions:
            if s.started:
                self.curGUI["Text"]["text"] += "    -> ID: " + str(s.ID) + " - Phase: " + str(s.phase) + "\n"
                self.curGUI["Text"]["text"] += "        -> ATK: " + str(s.attacker.peer.addr[1]) + "/" + str(s.attacker.name) + "\n"
                self.curGUI["Text"]["text"] += "            -> Money: " + str(s.attacker.money) + " - Units Active: " + str(len(s.attacker.manager.units)) + " - Units Last Save: " + str(len(s.attacker.manager.saves)) + "\n"
                self.curGUI["Text"]["text"] += "        -> DEF: " + str(s.defender.peer.addr[1]) + "/" + str(s.defender.name) + "\n"
                self.curGUI["Text"]["text"] += "            -> Money: " + str(s.defender.money) + " - Towers Active: " + str(len(s.defender.manager.towers)) + " - Towers Last Save: " + str(len(s.defender.manager.saves)) + "\n"
                self.curGUI["Text"]["text"] += "\n"